﻿namespace MvcFckEditor.Controllers
{
    using MvcFckEditor.Components;
    using MvcFckEditor.Models;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web.Mvc;

    public class FckEditorController : Controller
    {
        public ActionResult Browser(string currentFolder, string type)
        {
            Config config;
            currentFolder = (currentFolder ?? "").EndsWith("/") ? (currentFolder ?? "") : ((currentFolder ?? "") + "/");
            ViewData["CurrentFolder"] = currentFolder;
            ViewData["Type"] = type;
            string str5 = type;
            if (str5 != null)
            {
                if (!(str5 == "Image"))
                {
                    if (str5 == "Flash")
                    {
                        ViewData["PageTitle"] = "选择Flash";
                        goto Label_00CA;
                    }
                }
                else
                {
                    ViewData["PageTitle"] = "选择图片";
                    goto Label_00CA;
                }
            }
            ViewData["PageTitle"] = "选择文件";
        Label_00CA:
            config = new Config();
            string[] directories = Directory.GetDirectories(HttpContext.Server.MapPath(config.UserFilesPath + currentFolder));
            string[] files = Directory.GetFiles(HttpContext.Server.MapPath(config.UserFilesPath + currentFolder));
            List<FckItemInfo> model = new List<FckItemInfo>();
            if (currentFolder != "/")
            {
                model.Add(new FckItemInfo { Icon = config.FckBasePath + "Up.jpg", Height = 0x59, Width = 100, Name = "上层目录", Tip = "上层目录", Url = "/FckEditor/browser?type=" + type + "&currentFolder=" + currentFolder.Substring(0, currentFolder.Substring(0, currentFolder.Length - 1).LastIndexOf('/') + 1) });
            }
            foreach (string str in directories)
            {
                DirectoryInfo info2 = new DirectoryInfo(str);
                model.Add(new FckItemInfo { Icon = config.FckBasePath + "Folder.jpg", Height = 100, Width = 0x5e, Name = info2.Name, Tip = info2.Name, Url = "/FckEditor/browser?type=" + type + "&currentFolder=" + currentFolder + info2.Name });
            }
            foreach (string str2 in files)
            {
                FileInfo info4 = new FileInfo(str2);
                string str3 = info4.Extension.ToLower().Substring(1);
                if (config.TypeConfig[type].AllowedExtensions.Contains<string>(str3))
                {
                    try
                    {
                        System.Drawing.Image image = System.Drawing.Image.FromFile(str2);
                        model.Add(new FckItemInfo { Icon = config.UserFilesPath + currentFolder + info4.Name, Width = image.Width, Height = image.Height, Name = info4.Name, Url = "javascript:OpenFile('" + config.UserFilesPath + currentFolder + info4.Name + "');" });
                    }
                    catch
                    {
                        string path = config.FckBasePath + "filethumbnails/" + str3 + ".gif";
                        if (!System.IO.File.Exists(HttpContext.Server.MapPath(path)))
                        {
                            path = config.FckBasePath + "filethumbnails/unknown.gif";
                        }
                        model.Add(new FckItemInfo { Icon = path, Height = 0x20, Width = 0x20, Name = info4.Name, Tip = info4.Name, Url = "javascript:OpenFile('" + config.UserFilesPath + currentFolder + info4.Name + "');" });
                    }
                }
            }
            return View(model);
        }

        public ActionResult CreateFolder(string cFolder, string newFolder, string ctype)
        {
            if (!string.IsNullOrEmpty(Util.SanitizeFolderName(newFolder)))
            {
                cFolder = cFolder + Util.SanitizeFolderName(newFolder);
                Config config = new Config();
                Util.CreateDirectory(HttpContext.Server.MapPath(config.UserFilesPath + cFolder));
            }
            return RedirectToAction("Browser", "FckEditor", new { currentFolder = cFolder, type = ctype });
        }

        public string Upload()
        {
            string resourceType = Request.QueryString["Type"];
            Config config = new Config();
            return config.FileUpload(Request.Files["NewFile"], resourceType, "/", true);
        }

        public ActionResult UploadFile(string fFolder, string ftype)
        {
            TempData["Error"] = new Config().FileUpload(Request.Files["NewFile"], ftype, fFolder, false);
            return RedirectToAction("Browser", "FckEditor", new { currentFolder = fFolder, type = ftype });
        }
    }
}

